package com.validations;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.security.InvalidKeyException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;

import com.beans.*;
import com.exceptions.*;
import com.validations.*;

public class AllValidator 
{
	  private static Pattern namepattern;
	  private static Matcher namematcher;
	  private static final String NAME_PATTERN = "^[a-zA-Z][a-zA-Z]*$";
	  private static Pattern pattern;
	  private static Matcher matcher;
	  private static final String PASSWORD_PATTERN = "(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,12})$";
	  
	public static boolean isEmailValid(String emailId)
	{
		boolean isValid = false;

		//use reg ex to check if the email is valid or not!
		String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
		CharSequence email = emailId;
		
		//Make the comparison case-insensitive.
		Pattern patt = Pattern.compile(expression,Pattern.CASE_INSENSITIVE);
		Matcher m = patt.matcher(email);
		if(m.matches()){
		isValid = true;
		}
		
		return isValid;
	}
	
	/*
	 * check the credit card details are valid or not!
	 * check for the numeric value and 16 digits
	 */
	public boolean validateCreditcard(String number)
	{
		boolean isValid = false;
		try
		{
		Integer.parseInt(number);
		}catch (Exception e)
		{
		return isValid;
		}
		if(number.length() == 16)
		{
		isValid = true;
		return isValid;
		}else
		{
			return isValid;
		}
	}

	
	public static boolean validateUser(Person p) throws InvalidEmailException, MinimumPwdLengthException, InvalidKeyException, GeneralSecurityException, GeneralSecurityException, IOException
	{
		
		if(p.getEmailId() == null || !(isEmailValid(p.getEmailId())))
		{
			throw new InvalidEmailException();
		}
		if(p.getPassword()==null || (Encrypt.decrypt(p.getPassword().getBytes("iso-8859-1")).trim().length())<8){
			throw new MinimumPwdLengthException();
		}
		return true;
	}
	
	public boolean validateQuantity(int quantity) throws  NegativeValueException{
		if(quantity<0){
			throw new NegativeValueException();
		}	
		return true;
	}
		  //The following regular expression is for a 4 to 8 char password and containing at least an alphabet and one Number.

		  public static boolean validate(final String password)
		  {
			  try
			  {
				  pattern = Pattern.compile(PASSWORD_PATTERN);
				  matcher = pattern.matcher(password);
				  return matcher.matches();
			  }
			  catch(Exception e)
			  {
				  System.err.println(e);
				  return false;
			  }
		  }

		  public static boolean namevalidate(final String hex)
		  {
			  try
			  {
				  namepattern = Pattern.compile(NAME_PATTERN);
				  namematcher = pattern.matcher(hex);
				  return matcher.matches();
			  }
			  catch(Exception e)
			  {
				  System.err.println(e);
				  return false;
			  }
		  }
}

